package com.zhongen.quartz.util;

import cn.hutool.core.util.StrUtil;
import com.zhongen.common.core.util.SpringContextHolder;
import com.zhongen.quartz.constants.JobTypeQuartzEnum;
import com.zhongen.quartz.exception.TaskException;
import lombok.extern.slf4j.Slf4j;

/**
 * TaskInvok工厂类
 */
@Slf4j
public class TaskInvokeFactory {

	/**
	 * 根据对应jobType获取对应 invoker
	 */
	public static TaskInvoke getInvoker(String jobType) throws TaskException {
		if (StrUtil.isBlank(jobType)) {
			log.info("获取TaskInvoke传递参数有误，jobType:{}", jobType);
			throw new TaskException("");
		}

		TaskInvoke taskInvoke = null;
		if (JobTypeQuartzEnum.JAVA.getType().equals(jobType)) {
			taskInvoke = SpringContextHolder.getBean("javaClassTaskInvoke");
		}
		else if (JobTypeQuartzEnum.SPRING_BEAN.getType().equals(jobType)) {
			taskInvoke = SpringContextHolder.getBean("springBeanTaskInvoke");
		}
		else if (JobTypeQuartzEnum.REST.getType().equals(jobType)) {
			taskInvoke = SpringContextHolder.getBean("restTaskInvoke");
		}
		else if (JobTypeQuartzEnum.JAR.getType().equals(jobType)) {
			taskInvoke = SpringContextHolder.getBean("jarTaskInvoke");
		}
		else if (StrUtil.isBlank(jobType)) {
			log.info("定时任务类型无对应反射方式，反射类型:{}", jobType);
			throw new TaskException("");
		}

		return taskInvoke;
	}

}
